package cn.nuomi.shop.api.controller;

import cn.nuomi.shop.api.entity.User;
import cn.nuomi.shop.api.service.UserService;
import cn.nuomi.shop.api.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @date: 2021/4/29 20:23
 * @description: 钱包功能
 */

@WebServlet("/walletServlet")
public class WalletServlet extends BaseServlet{

    private UserService user = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try{
            String userName = req.getParameter("userName");

            String balance = req.getParameter("totalNumber");

            BigDecimal bd =new BigDecimal(balance);

            resp.setHeader("Access-Control-Allow-Origin", "*");

            User user = this.user.changeBalance(userName, bd);

            successResp(resp,user);
        }catch (RuntimeException e){
            e.printStackTrace();
            errorResp(resp,e.getMessage());
        }
    }
}
